package com.ly.spring.mvc.web;

import com.ly.spring.mvc.entity.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@Component
// 控制器增强注解
// RestController 前缀表示该类所有的方法返回 json 数据
@RestControllerAdvice
public class ExAdvice {

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Result maxUploadSizeExceededException(MaxUploadSizeExceededException ex){
        String msg = "上传文件大小不能超过 %s ";
        msg = String.format(msg, "1MB");
        return new Result(0, msg, null);
    }

    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        e.printStackTrace();
        // 发送短息或邮件, 通知管理员处理
        // 通知客服
        return new Result(-1, "系统繁忙,稍后再试", null);
    }

}
